<?php

namespace App\Admin\Controllers;

use App\Models\QdcsClass;
use \App\Models\QdcsProject;

use com_jjcbs\lib\ServiceFactory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use ext\service\ClassServiceImpl;

class ProjectController extends Controller
{
    use ModelForm;

    private $classService = null;

    public function __construct()
    {
        $this->classService = ServiceFactory::getInstance(ClassServiceImpl::class);
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('项目管理');
            $content->description('项目列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('项目管理');
            $content->description('编辑');
            $content->disablePjax(true);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('项目管理');
            $content->description('创建');
            $content->disablePjax(true);
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(QdcsProject::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('img' ,'主图')->display(function ($img){
                return "<img src='" . config('admin.upload.host') . $img . "' width='50' height='50'>";
            });
            $grid->name('项目名称');
            $grid->body_name('单位名称');
            $grid->column('ptype_id' , '所属分类')->display(function($ptype_id){
                return ServiceFactory::getInstance(ClassServiceImpl::class)->classDaoImpl
                    ->getOne(QdcsClass::class , $ptype_id)->name;
            });
            $grid->period('项目周期(年)');
            $grid->plance('项目施工地址');
            $grid->receiver('援助对象');
            $grid->amount('援助金额');
            $grid->principal('联系人');
            $grid->principal_phone('联系人电话');
            $grid->principal_address('联系人地址');
            $grid->principal_work_time('联系人工作时间');

            $grid->created_at('创建时间');
            $grid->updated_at('编辑时间');

            // add filter
            $grid->filter(function (Grid\Filter $filter){
               $filter->like('name' , '项目名');
               $filter->date('created_at' , '创建时间');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(QdcsProject::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name' , '项目名称')->rules('required');
            $form->image('img' , '项目主图');
            $form->select('ptype_id' , '项目分类')->options($this->classService->getChildOption(29));
            $form->text('body_name' , '单位名称')->rules('required');
            $form->text('period' , '项目周期(年)')->rules('required');
            $form->text('plance', '项目施工地址')->rules('required');
            $form->text('receiver', '援助对象')->rules('required');
            $form->decimal('amount','援助金额')->rules('required');
            $form->text('principal','联系人');
            $form->text('principal_phone','联系人电话');
            $form->text('principal_address','联系人地址');
            $form->text('principal_work_time','联系人工作时间');
            $form->ueditor('procedure_text','申请途经及程序');
            $form->ueditor('information','项目申请材料');
        });
    }
}
